using System.Collections.Generic;
using BasicScript.Component;
using Game.Const;
using Game.Core;
using Game.Main.Model;
using Game.Main.View;
using Game.Manager;
using Game.XH;
using UnityEngine;

namespace Game.Component
{
	public class SlotMachineItem : BaseItem
	{
		public enum Anim
		{
			Static = 0,
			Roll = 1,
			Success = 2
		}

		private const float MIN_SPEED = 2f;

		private const float MAX_SPEED = 7.8f;

		private const float ROLL_TIME = 39f;

		private const float LINE_Y = 2.47f;

		private Anim anim;

		private Transform line1;

		private SpriteRenderer[] line1Sprites;

		private int line1NewIcon;

		private float line1Y;

		private float line1Timer;

		private float line1Speed;

		private bool line1SpeedUp;

		private int line1Counter;

		private Transform line2;

		private SpriteRenderer[] line2Sprites;

		private int line2NewIcon;

		private float line2Y;

		private float line2Timer;

		private float line2Speed;

		private bool line2SpeedUp;

		private int line2Counter;

		private Transform line3;

		private SpriteRenderer[] line3Sprites;

		private int line3NewIcon;

		private float line3Y;

		private float line3Timer;

		private float line3Speed;

		private bool line3SpeedUp;

		private int line3Counter;

		private SpriteRenderer[] itemIcons;

		private string createItemId;

		private FrameAnimation successAnim;

		private FrameAnimation leftLight;

		private FrameAnimation rightLight;

		private FrameAnimation[] topLights;

		private Transform lightTrans;

		private float lightTimer;

		private int topLightIndex;

		private bool isShake;

		private int shakeStage;

		private float shakeTimer;

		public override void Init(ItemArchiveData data)
		{
			base.Init(data);
			Transform trans = model.Find("Mask");
			trans.SetLocalZ(-2E-05f);
			lightTrans = model.Find("Light");
			lightTrans.SetLocalZ(-3E-05f);
			leftLight = lightTrans.Find("LightLeft").GetComponent<FrameAnimation>();
			rightLight = lightTrans.Find("LightRight").GetComponent<FrameAnimation>();
			successAnim = lightTrans.GetComponent<FrameAnimation>();
			Transform trans2 = model.Find("BackMask1");
			trans2.SetLocalZ(-1E-05f);
			Transform trans3 = model.Find("BackMask2");
			trans3.SetLocalZ(1E-05f);
			line1 = model.Find("Line1");
			line2 = model.Find("Line2");
			line3 = model.Find("Line3");
		}

		protected override void LateUpdate()
		{
			if (anim == Anim.Static)
			{
				return;
			}
			if (isShake)
			{
				shakeTimer += Time.deltaTime * 6f;
				if (shakeTimer >= 1f)
				{
					shakeTimer = 1f;
				}
				if (shakeStage == 0)
				{
					model.SetLocalScaleXY(1f + 0.05f * shakeTimer / 1f, 1f - 0.05f * shakeTimer / 1f);
				}
				else
				{
					model.SetLocalScaleXY(1.05f - 0.1f * shakeTimer / 1f, 0.95f + 0.05f * shakeTimer / 1f);
				}
				if (shakeTimer >= 1f)
				{
					shakeStage++;
					if (shakeStage > 0)
					{
						isShake = false;
						SetAllCollidersEnabled(true);
						model.SetLocalScaleXY(1f, 1f);
					}
				}
			}
			if (anim == Anim.Roll)
			{
				AddSpeed(ref line1Speed, ref line1SpeedUp);
				AddTimer(ref line1Timer, line1Speed);
				UpdateLine(line1, line1Sprites, line1NewIcon, line1Y, line1Timer, ref line1Counter);
				if (line1Timer > 3.5f)
				{
					AddSpeed(ref line2Speed, ref line2SpeedUp);
					AddTimer(ref line2Timer, line2Speed);
					UpdateLine(line2, line2Sprites, line2NewIcon, line2Y, line2Timer, ref line2Counter);
					if (line2Timer > 3.5f)
					{
						AddSpeed(ref line3Speed, ref line3SpeedUp);
						AddTimer(ref line3Timer, line3Speed);
						UpdateLine(line3, line3Sprites, line3NewIcon, line3Y, line3Timer, ref line3Counter);
						if (line3Timer >= 39f)
						{
							ResetLine(line1);
							ResetLine(line2);
							ResetLine(line3);
							if (string.IsNullOrEmpty(createItemId))
							{
								StopAnim();
								Singleton<AudioManager>.Instance.PlaySound("sound_slot_machine_failure");
							}
							else
							{
								anim = Anim.Success;
								line1Timer = 0f;
							}
						}
					}
				}
			}
			else if (anim == Anim.Success)
			{
				line1Timer += Time.deltaTime;
				if (line1Timer > 0.5f && !string.IsNullOrEmpty(createItemId))
				{
					Singleton<AudioManager>.Instance.PlaySound("sound_slot_machine_success");
					CreateGashapon();
					createItemId = string.Empty;
					StopAnim();
				}
			}
			lightTimer += Time.deltaTime;
			if (lightTimer >= 0.2f)
			{
				lightTimer = 0f;
				topLightIndex++;
				if (topLightIndex >= topLights.Length)
				{
					topLightIndex = 0;
				}
				topLights[topLightIndex].Play();
			}
		}

		private void StopAnim()
		{
			anim = Anim.Static;
			leftLight.Stop();
			leftLight.GotoFirstFrame();
			rightLight.Stop();
			rightLight.GotoFirstFrame();
			RemoveAudioPlayer();
		}

		private void ResetLine(Transform line)
		{
			int childCount = line.childCount;
			line.SetLocalY(2.47f);
			for (int i = 0; i < childCount; i++)
			{
				Transform child = line.GetChild(i);
				switch (i)
				{
				case 0:
					child.SetLocalY(0.5f);
					break;
				case 1:
					child.SetLocalY(0f);
					break;
				case 2:
					child.SetLocalY(-0.5f);
					break;
				}
			}
		}

		private void AddSpeed(ref float speed, ref bool isSpeedUp)
		{
			if (isSpeedUp)
			{
				if (speed < 7.8f)
				{
					speed += Time.deltaTime * 6f;
					if (speed >= 7.8f)
					{
						speed = 7.8f;
						isSpeedUp = false;
					}
				}
			}
			else if (speed > 2f)
			{
				speed -= Time.deltaTime;
				if (speed <= 2f)
				{
					speed = 2f;
				}
			}
		}

		private void AddTimer(ref float timer, float speed)
		{
			if (timer < 39f)
			{
				timer += Time.deltaTime * speed;
				if (timer >= 39f)
				{
					timer = 39f;
				}
			}
		}

		private void UpdateLine(Transform line, SpriteRenderer[] sprites, int newIcon, float y, float timer, ref int counter)
		{
			y = (0f - timer) / 2f;
			line.SetLocalY(y + 2.47f);
			if ((double)(timer % 1f) >= 0.4 && counter < (int)timer)
			{
				Transform child = line.GetChild(0);
				Transform child2 = line.GetChild(2);
				child2.localPosition = new Vector3(0f, child.localPosition.y + 0.5f, 0f);
				child2.SetSiblingIndex(0);
				ChangeIcon(child2, sprites, timer, newIcon);
				counter++;
			}
		}

		private void ChangeIcon(Transform item, SpriteRenderer[] sprites, float timer, int newIcon)
		{
			if ((float)(int)timer == 37f)
			{
				item.GetComponent<SpriteRenderer>().sprite = itemIcons[newIcon].sprite;
			}
			else
			{
				if ((float)(int)timer != 36f && (float)(int)timer != 35f)
				{
					return;
				}
				List<SpriteRenderer> list = new List<SpriteRenderer>();
				for (int i = 0; i < itemIcons.Length; i++)
				{
					bool flag = false;
					for (int j = 0; j < sprites.Length; j++)
					{
						if (itemIcons[i].sprite.name.Equals(sprites[j].sprite.name))
						{
							flag = true;
							break;
						}
					}
					if (!flag)
					{
						list.Add(itemIcons[i]);
					}
				}
				item.GetComponent<SpriteRenderer>().sprite = list[Random.Range(0, list.Count)].sprite;
			}
		}

		private void CreateGashapon()
		{
			if (!string.IsNullOrEmpty(createItemId) && base.itemData.createIds != null && archiveData.createIndexs.Count < base.itemData.createAmount)
			{
				BaseItem baseItem = Singleton<Scene>.Instance.CreateItem(createItemId, base.transform.position + new Vector3(-0.03f, 0.61f), 0);
				AttachItem(baseItem, DepthType.Ahead, AttachType.Place, false);
				baseItem.depthSystem.SortBaseNode();
				AddCreateItem(baseItem);
				Singleton<AudioManager>.Instance.PlaySound("sound_explode");
				Singleton<EffectManager>.Instance.ShowEffect(PathConst.EFFECT + "efc_explode_1", baseItem.transform.position + new Vector3(0f, 0.8f, -0.0004f), 1f);
				Singleton<EffectManager>.Instance.ShowEffect(PathConst.EFFECT + "efc_blink_1", baseItem.transform.position + new Vector3(0f, 0.6f, -0.0004f), 3f);
			}
		}

		public override void QuicklyTouch(TouchEventData touchEventData)
		{
			base.QuicklyTouch(touchEventData);
			if (anim != 0)
			{
				return;
			}
			SetAllCollidersEnabled(false);
			model.Find("Rocker").GetComponent<FrameAnimation>().Play();
			Singleton<AudioManager>.Instance.PlaySound("sound_slot_machine_1");
			Singleton<AudioManager>.Instance.PlayAudio("sound_slot_machine_2", true, GetAudioPlayer());
			InitParams();
			anim = Anim.Roll;
			line1Timer = 0f;
			line2Timer = 0f;
			line3Timer = 0f;
			line1Counter = -1;
			line2Counter = -1;
			line3Counter = -1;
			line1Y = 0f;
			line2Y = 0f;
			line3Y = 0f;
			line1Speed = 0f;
			line2Speed = 0f;
			line3Speed = 0f;
			line1SpeedUp = true;
			line2SpeedUp = true;
			line3SpeedUp = true;
			isShake = true;
			shakeTimer = 0f;
			shakeStage = 0;
			leftLight.Play();
			rightLight.Play();
			lightTimer = 0f;
			topLights = new FrameAnimation[3];
			topLightIndex = 0;
			for (int i = 0; i < 3; i++)
			{
				topLights[i] = lightTrans.Find("LightTop" + (i + 1)).GetComponent<FrameAnimation>();
			}
			topLights[0].Play();
			bool flag = false;
			if (Random.Range(0, 100) < 38)
			{
				flag = true;
			}
			createItemId = string.Empty;
			if (flag)
			{
				line1NewIcon = Random.Range(0, itemIcons.Length);
				line2NewIcon = line1NewIcon;
				line3NewIcon = line1NewIcon;
				createItemId = itemIcons[line1NewIcon].transform.name;
				return;
			}
			line1NewIcon = Random.Range(0, itemIcons.Length);
			if (Random.Range(0, 100) < 50)
			{
				line2NewIcon = GetRandomIndex(0, itemIcons.Length, line1NewIcon);
				line3NewIcon = Random.Range(0, itemIcons.Length);
			}
			else
			{
				line2NewIcon = Random.Range(0, itemIcons.Length);
				line3NewIcon = GetRandomIndex(0, itemIcons.Length, line1NewIcon);
			}
		}

		private int GetRandomIndex(int min, int max, int except)
		{
			int num = Random.Range(min, max);
			if (num == except)
			{
				return GetRandomIndex(min, max, except);
			}
			return num;
		}

		private void InitParams()
		{
			if (itemIcons == null)
			{
				Transform transform = model.Find("Items");
				int childCount = transform.childCount;
				itemIcons = new SpriteRenderer[childCount];
				for (int i = 0; i < childCount; i++)
				{
					itemIcons[i] = transform.GetChild(i).GetComponent<SpriteRenderer>();
				}
				InitLine(line1, out line1Sprites);
				InitLine(line2, out line2Sprites);
				InitLine(line3, out line3Sprites);
			}
		}

		private void InitLine(Transform line, out SpriteRenderer[] lineSprites)
		{
			int childCount = line.childCount;
			lineSprites = new SpriteRenderer[childCount];
			for (int i = 0; i < childCount; i++)
			{
				lineSprites[i] = line.GetChild(i).GetComponent<SpriteRenderer>();
			}
		}
	}
}
